#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
 
#define SOCKET_PATH "/tmp/unix_socket"
#define BUFFER_SIZE 1024
 
int main() {
    int sock = 0;
    struct sockaddr_un serv_addr;
    char buffer[BUFFER_SIZE] = {0};
    char *hello = "Hello from client";
 
    // 创建套接字
    if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation error");
        return -1;
    }
 
    serv_addr.sun_family = AF_UNIX;
    strcpy(serv_addr.sun_path, SOCKET_PATH);
 
    // 连接服务端
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr_un)) < 0) {
        perror("Connection Failed");
        return -1;
    }
 
    // 发送数据到服务端
    send(sock, hello, strlen(hello), 0);
    printf("Hello message sent\n");
 
    // 读取服务端的响应
    int valread = read(sock, buffer, BUFFER_SIZE);
    printf("Received: %s\n", buffer);
 
    // 关闭套接字
    close(sock);
 
    return 0;
}